<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Rukan <rukanhitokiri@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
class ZhelpdeskadminTab extends AdminTab
{	
	/**
	 * 
	 * Load TinyMce editor
	 * 
	 * @return string html
	 */
	public function loadTinyMCE()
	{
		global $cookie;
		$defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
		$languages = Language::getLanguages(false);
		$iso = Language::getIsoById((int)$cookie->id_lang);
		$content = '';
		if (version_compare(_PS_VERSION_, '1.4.0.0') >= 0)
			$content .= '
			<script type="text/javascript">	
				var iso = \''.(file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en').'\' ;
				var pathCSS = \''._THEME_CSS_DIR_.'\' ;
				var ad = \''.dirname($_SERVER['PHP_SELF']).'\' ;
			</script>
			<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
			<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>
			<script language="javascript">id_language = Number('.$defaultLanguage.');</script>';
		else
		{
			$content .= '
			<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
			<script type="text/javascript">
				tinyMCE.init({
					mode : "textareas",
					theme : "advanced",
					plugins : "safari,pagebreak,style,layer,table,advimage,advlink,inlinepopups,media,searchreplace,contextmenu,paste,directionality,fullscreen",
					theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
					theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,,|,forecolor,backcolor",
					theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,media,|,ltr,rtl,|,fullscreen",
					theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,pagebreak",
					theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "left",
					theme_advanced_statusbar_location : "bottom",
					theme_advanced_resizing : false,
					content_css : "'.__PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/global.css",
					document_base_url : "'.__PS_BASE_URI__.'",
					width: "600",
					height: "auto",
					font_size_style_values : "8pt, 10pt, 12pt, 14pt, 18pt, 24pt, 36pt",
					template_external_list_url : "lists/template_list.js",
					external_link_list_url : "lists/link_list.js",
					external_image_list_url : "lists/image_list.js",
					media_external_list_url : "lists/media_list.js",
					elements : "nourlconvert",
					entity_encoding: "raw",
					convert_urls : false,
					language : "'.(file_exists(_PS_ROOT_DIR_.'/js/tinymce/jscripts/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en').'"
				});
				id_language = Number('.$defaultLanguage.');
			</script>';
		}
		return $content;
	}
	
	/**
	 * 
	 * load labrary jquery color picker
	 * 
	 * @return string html
	 */
	public static function loadJsColorPicker()
	{
		return '<script type="text/javascript" src="'._PS_JS_DIR_.'/jquery/jquery-colorpicker.js"></script>';
	}
	
	/**
	 * add element for form
	 *
	 * @param string $type type element html render
	 * @param array	$fields
     * @return string html
	 */
	public function addElement($type, $fields = array(),$lang=false)
	{	
		if ($type == 'text')
			return $this->renderTextInput($fields,$lang);
		elseif ($type == 'color')
			return $this->renderColorPicker($fields,$lang);
		elseif ($type == 'select')
			return $this->renderSelect($fields,$lang);
		elseif ($type == 'yesno')
			return $this->renderYesNoOption($fields,$lang);
		elseif ($type == 'area')	
			return $this->renderArea ($fields,$lang);
		elseif ($type == 'file')
			return $this->renderUpFile($fields);
		elseif ($type == 'view')	
			return $this->renderTextView($fields);
		
	}
	
	/**
	 * display Color Picker
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderColorPicker($fields = array())
	{
		$obj = $this->loadObject(true);
		// <script type="text/javascript" src="'._PS_JS_DIR_.'/jquery/jquery-colorpicker.js"></script>
		$_html = '
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<input type="color" width="50px" data-hex="true" class="color mColorPickerInput" name="'.$fields['name'].'" value="'.htmlentities($this->getFieldValue($obj, $fields['name']), ENT_COMPAT, 'UTF-8').'" />
		</div>
		<div class="clear"></div>';
	}
	
	/**
	 * display TextInput
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderTextInput($fields = array(),$lang=false)
	{	
		$obj = $this->loadObject(true);
		if (!$lang) {
			return '	
			<label>'.$this->l($fields['label']).'</label>
			<div class="margin-form">
				<input '.(array_key_exists ('style',$fields)==true?'style="'.$fields['style'].'"':'').' name="'.$fields['name'].'" '.(array_key_exists ('more',$fields)==true?$fields['more'] : '').' id="'.$fields['name'].'" type="text" class="text" '.(array_key_exists ('disabled',$fields)==true ? (isset($obj->id) ? 'disabled="disabled"': '' ) : '').' value="'.$this->getFieldValue($obj, $fields['name']).'"/>
				'.(array_key_exists ('expand',$fields)==true?$this->l($fields['expand']):'').'
				<p class="clear">'.(array_key_exists ('note',$fields)==true ? $this->l($fields['note']) : '').'</p>
			</div>
			<div class="clear"></div>';
		}
		else {
			echo '<label>'.$this->l($fields['label']).':</label>
				<div class="margin-form">';
			foreach ($this->_languages as $language)
			echo '
					<div id="'.$fields['name'].'_'.$language['id_lang'].'" style="float: left;'.($language['id_lang'] != $this->_defaultFormLanguage ? 'display:none;' : '').'">
						<input '.(array_key_exists ('style',$fields)==true?'style="'.$fields['style'].'"':'').' id="'.$fields['name'].'_'.$language['id_lang'].'"   '.$fields['more'].'  name="'.$fields['name'].'_'.$language['id_lang'].'" type="text" class="text" value="'.htmlentities(stripslashes($this->getFieldValue($obj, $fields['name'], $language['id_lang'])), ENT_COMPAT, 'UTF-8').'"/><sup> *</sup>
						<p class="clear">'.(array_key_exists ('note',$fields)==true ? $this->l($fields['note']) : '').'</p>
					</div>';
			$this->displayFlags($this->_languages, $this->_defaultFormLanguage, $fields['langtags'], $fields['name']);
			
			echo '
			<div class="clear"> </div></div>';
		}
	}
	
		
	/**
	 * display Select option
	 * 
	 * @param array	$fields
     * @return string html
	 */
	public function renderSelect($fields = array())
	{
		$obj = $this->loadObject(true);
		
		$html = '	
				<label>'.$this->l($fields['label']).'</label>
				<div class="margin-form">
					<select name="'.$fields['name'].'" id="'.$fields['name'].'" '.(array_key_exists ('style',$fields)==true?'style="'.$this->l($fields['style']).'"':'').'  '.(array_key_exists ('disabled',$fields)==true ? (isset($obj->id) ? 'disabled="disabled"': '' ) : '').' >
						'.(array_key_exists('value0', $fields)==true ? '<option value="0">&nbsp;&nbsp;'.$this->l($fields['value0']).'</option>' : '');
			foreach($fields['values'] as $value){
				if (array_key_exists ('value',$value))
					$html .= '	<option value="'.$value['value'].'" '.($this->getFieldValue($obj, $fields['name']) == $value['value'] ? "selected='selected'" : '').'>&nbsp;&nbsp;'.$value['name'].'&nbsp;&nbsp;</option>';
				else 
					$html .= '	<option value="'.$value['name'].'" '.($this->getFieldValue($obj, $fields['name']) == $value['name'] ? "selected='selected'" : '').'>&nbsp;&nbsp;'.$value['name'].'&nbsp;&nbsp;</option>';
			}
		
		$html .= '	</select> 
					'.(array_key_exists ('expand',$fields)==true?$this->l($fields['expand']):'').'
					<p class="clear">'.(array_key_exists('note',$fields)==true ? $this->l($fields['note']) : '').'</p>
				</div>
				<div class="clear"></div>';
		return $html;
	}
	
	/**
	 * display YesNo option radio option
	 * 
	 * @param array	$fields
     * @return string html
	 */
	public function renderYesNoOption($fields = array())
	{
		if (!($obj = $this->loadObject(true)))
			return;
		return '	
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<input type="radio" name="'.$fields['name'].'" id="'.$fields['name'].'_on" value="1" '.($this->getFieldValue($obj, $fields['name']) ? 'checked="checked" ' : '').'/>
			<label class="t" for="'.$fields['name'].'_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
			<input type="radio" name="'.$fields['name'].'" id="'.$fields['name'].'_off" value="0" '.(!$this->getFieldValue($obj, $fields['name']) ? 'checked="checked" ' : '').'/>
			<label class="t" for="'.$fields['name'].'_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
		</div>
		<div class="clear"></div>';
	}
	/**
	 * 
	 * Display textarea with multi lang or no lang
	 * @param array $fields
	 * @param bool $lang
	 */
	public function renderArea ($fields = array (),$lang = false) 
	{
		$obj = $this->loadObject(true);
		echo '
			<label>'.$this->l($fields['label']).'</label>
			<div class="margin-form">';
		if ($lang) {
			
			foreach ($this->_languages as $language)
			echo '
					<div id="'.$fields['name'].'_'.$language['id_lang'].'" style="float: left;'.($language['id_lang'] != $this->_defaultFormLanguage ? 'display:none;' : '').'">
						<textarea class="rte" name="'.$fields['name'].'_'.$language['id_lang'].'" rows="10" cols="100">'.htmlentities($this->getFieldValue($obj, $fields['name'], (int)($language['id_lang'])), ENT_COMPAT, 'UTF-8').'</textarea> <sup> *</sup>
					</div>';
			$this->displayFlags($this->_languages, $this->_defaultFormLanguage, $fields['langtags'], $fields['name']);
			
		}else{
			echo '
						<textarea class="rte" name="'.$fields['name'].'" rows="30" cols="70">'.htmlentities($this->getFieldValue($obj, $fields['name']), ENT_COMPAT, 'UTF-8').'</textarea>
						'.(array_key_exists ('expand',$fields)==true?$this->l($fields['expand']):'').'
				';
		}
		echo '	
			</div>		
			<div class="clear"></div>
			';
	}
	
	/**
	 * display Color Picker
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderUpFile($fields = array())
	{
		$obj = $this->loadObject(true);
		$_html = '
			<label>'.$this->l($fields['label']).'</label>
			<div class="margin-form">
				<input '.(array_key_exists ('style',$fields)==true?'style="'.$fields['style'].'"':'').' name="'.$fields['name'].'" '.(array_key_exists ('more',$fields)==true?$fields['more'] : '').' id="'.$fields['name'].'" type="file" class="text" value="'.$this->getFieldValue($obj, $fields['name']).'"/>
				'.(array_key_exists ('expand',$fields)==true?$this->l($fields['expand']):'').'
				<p class="clear">'.(array_key_exists ('note',$fields)==true ? $this->l($fields['note']) : '').'</p>
			</div>
			<div class="clear"></div>	';
		return $_html;
	}
	
/**
	 * display Text view
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderTextView($fields = array())
	{	
		$obj = $this->loadObject(true);
		return '	
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<p '.(array_key_exists ('style',$fields)==true?'style="'.$fields['style'].'"':'').' '.(array_key_exists ('more',$fields)==true?$fields['more'] : '').' > '.$this->getFieldValue($obj, $fields['name']).' </p>
			'.(array_key_exists ('expand',$fields)==true?$this->l($fields['expand']):'').'
		</div>
		<div class="clear"></div>';
	}
}